package com.br.aiops.utils;

import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Date;

public class DateUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

    public static class Duration {

        private long millis;

        public Duration(Date start, Date end) {
            this.millis = Math.abs(end.getTime() - start.getTime());
        }

        public long toSeconds() {
            return millis / 1000;
        }

        public long toMinutes() {
            return toSeconds() / 60;
        }

        public long toHours() {
            return toMinutes() / 60;
        }

        public long toDays() {
            return toHours() / 24;
        }
    }

    public static Duration duration(Date start, Date end) {
        return new Duration(start, end);
    }

    public static Date parse(String pattern, String str) {
        try {
            return FastDateFormat.getInstance(pattern).parse(str);
        } catch (ParseException e) {
            LOGGER.error("字符串{}转换日期格式错误。", str, e);
        }
        return null;
    }
}
